package com.example.administrator.kotiln.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.adapter.MyGridViewAdpter;
import com.example.administrator.kotiln.adapter.MyViewPagerAdapter;
import com.example.administrator.kotiln.bean.ProdctBean;
import com.example.administrator.kotiln.utils.Share;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/27.
 */


public class ViewPagerGrildViewActvity extends AppCompatActivity {
    private ViewPager viewPager;
    private LinearLayout group;//圆点指示器
    private ImageView[] ivPoints;//小圆点图片的集合
    private int totalPage; //总的页数
    private int mPageSize = 8; //每页显示的最大的数量
    private List<ProdctBean> listDatas;//总的数据源
    private List<View> viewPagerList;//GridView作为一个View对象添加到ViewPager集合中
    //private int currentPage;//当前页

    int position = 50000;
    private boolean isTouched = false;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            viewPager.setCurrentItem(position);
        }

    };

    private String[] proName = {"名称0", "名称1", "名称2", "名称3", "名称4", "名称5",
            "名称6", "名称7", "名称8", "名称9", "名称10", "名称11", "名称12", "名称13",
            "名称14", "名称15", "名称16", "名称17", "名称18", "名称19"};
    private Thread change;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpagergrildview);
        //初始化控件
        initView();
        //添加业务逻辑
        initData();


        //实现思路二
        // TODO Auto-generated method stub
// TODO Auto-generated catch block
        change = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    if (!isTouched) {
                        position++;
                        mHandler.sendEmptyMessage(0);
                        Share.d("睡眠的po"+position);
                    }
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        change.start();

    }


    private void initView() {
        // TODO Auto-generated method stub
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        group = (LinearLayout) findViewById(R.id.points);
        listDatas = new ArrayList<ProdctBean>();
        for (int i = 0; i < proName.length; i++) {
            listDatas.add(new ProdctBean(proName[i], R.drawable.iv_driving));
        }
    }

    private void initData() {
        // TODO Auto-generated method stub
        //总的页数向上取整
        totalPage = (int) Math.ceil(listDatas.size() * 1.0 / mPageSize);
        viewPagerList = new ArrayList<View>();
        for (int i = 0; i < totalPage; i++) {
            //每个页面都是inflate出一个新实例
            final GridView gridView = (GridView) View.inflate(this, R.layout.item_girldview, null);
            gridView.setAdapter(new MyGridViewAdpter(this, listDatas, i, mPageSize));
//            //添加item点击监听
//            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//                @Override
//                public void onItemClick(AdapterView<?> arg0, View arg1,
//                                        int position, long arg3) {
//                    // TODO Auto-generated method stub
//                    Object obj = gridView.getItemAtPosition(position);
//                    if (obj != null && obj instanceof ProdctBean) {
//                        System.out.println(obj);
//                        Toast.makeText(ViewPagerGrildViewActvity.this, ((ProdctBean) obj).getName(), Toast.LENGTH_SHORT).show();
//                    }
//                }
//            });
            //每一个GridView作为一个View对象添加到ViewPager集合中
            viewPagerList.add(gridView);
        }
        //设置ViewPager适配器
        viewPager.setAdapter(new MyViewPagerAdapter(viewPagerList));
        viewPager.setCurrentItem(position);
        Share.d("加载完毕的po"+position);


//        //实现思路三
//       viewPager.setOnTouchListener(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View arg0, MotionEvent event) {
//                // TODO Auto-generated method stub
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                    case MotionEvent.ACTION_MOVE:
//                        isTouched = true;
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        isTouched = false;
//                    default:
//                        break;
//                }
//                return false;
//            }
//        });
//       viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//
//            @Override
//            public void onPageSelected(int arg0) {
//                // TODO Auto-generated method stub
//                position = arg0;
//            }
//
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//                // TODO Auto-generated method stub
//
//            }
//        });
//


//        //添加小圆点
//        ivPoints = new ImageView[totalPage];
//        for (int i = 0; i < totalPage; i++) {
//            //循坏加入点点图片组
//            ivPoints[i] = new ImageView(this);
//            if (i == 0) {
//                ivPoints[i].setImageResource(R.drawable.page_focuese);
//            } else {
//                ivPoints[i].setImageResource(R.drawable.page_unfocused);
//            }
//            ivPoints[i].setPadding(8, 8, 8, 8);
//            group.addView(ivPoints[i]);
//        }
//        //设置ViewPager的滑动监听，主要是设置点点的背景颜色的改变
//        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
//            @Override
//            public void onPageSelected(int position) {
//                // TODO Auto-generated method stub
//                //currentPage = position;
//                for (int i = 0; i < totalPage; i++) {
//                    if (i == position) {
//                        ivPoints[i].setImageResource(R.drawable.page_focuese);
//                    } else {
//                        ivPoints[i].setImageResource(R.drawable.page_unfocused);
//                    }
//                }
//            }
//        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        change.destroy();
    }
}
